//
//  SimpleStopWatchVC.swift
//  Swift_Study
//
//  Created by apple on 2018/9/26.
//  Copyright © 2018年 dminfo. All rights reserved.
//

import UIKit

class SimpleStopWatchVC: UIViewController {

  @IBOutlet weak var playBtn: UIButton!
  
  @IBOutlet weak var pauseBtn: UIButton!
  
  @IBOutlet weak var timeLabel: UILabel!
  //数据
  var counter = 0.0
  var timer = Timer()
  var isPlaying = false
  
    override func viewDidLoad() {
        super.viewDidLoad()
     title = "SimpleStopWatch"
     timeLabel.text = String(counter)

//1.字符串占位
//      let name = "蒋俊"
//      print("我的名字是\(name)")
//2.可选类型
     let strNumber = "12kkk"
      //intNumber就是一个可选类型
      let intNumber = Int(strNumber)
      if intNumber != nil {
        print("转换成功")
      }else{
        print("转换失败")
      }
    }
//3.可选绑定
 
//4.
  
  
  //重置按钮事件
  @IBAction func resetButtonDidTouch(_ sender: UIButton) {
      timer.invalidate()
      isPlaying = false
      counter = 0
      timeLabel.text = String(counter)
      playBtn.isEnabled = true
      pauseBtn.isEnabled = true
  }
  //播放
  @IBAction func playButtonDidTouch(_ sender: UIButton) {
    if(isPlaying){
      return
    }
    playBtn.isEnabled = false
    pauseBtn.isEnabled = true
    timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.UpdateTimer), userInfo: nil, repeats: true)
    
  }
  //暂停
  @IBAction func pauseButtonDidTouch(_ sender: UIButton) {
    playBtn.isEnabled = true
    pauseBtn.isEnabled = false
    timer.invalidate()
    isPlaying = false
  }
  //更新timer
  @objc func UpdateTimer() {
    counter = counter + 0.1
    timeLabel.text = String(format: "%.1f", counter)
  }

}
